#include <iostream>
#include <exception>
#include <string>

class MyException:public std::exception
{
public:
    MyException(const char* error):errorString(error){}
    virtual const char* what()const noexcept//noexcept:不会抛出任何异常
    {
        return errorString.c_str();
    }
private:
    std::string errorString;
};

void func()
{
    throw MyException("自定义异常");
}

int main()
{
    try
    {
        func();
    }
    catch(const std::exception& e)
    {
        //输出错误信息
        std::cout << e.what() << std::endl;
    }
    
    return 0;
}